﻿document.write('<script language="javascript" src= "/HtmlCss/BaseJavaScript.js"></script>');

function test() {
    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/Basemap",
        "esri/core/urlUtils",
        "esri/geometry/Extent",
        "esri/geometry/Point",
        "esri/layers/GraphicsLayer",
        "esri/layers/MapImageLayer",
        "esri/views/MapView",
        "esri/Graphic",
        "esri/Color",
        "esri/symbols/PictureFillSymbol",
        "esri/geometry/SpatialReference",
    ],
        function (Map,
            MapView,
            Basemap,
            urlUtils,
            Extent,
            Point,
            GraphicsLayer,
            MapImageLayer,
            MapView,
            Graphic,
            Color,
            PictureFillSymbol,
            SpatialReference) {


            $.ajax({
                url: '/ashxs/getquhua.ashx',
                type: 'GET',
                data: {
                    "qhid": 24
                },
                success: function (data) {
                    var quhua = JSON.parse(data);

                    console.log("quhua is \w\n" + quhua.length);
                    var centershape = quhua[0].CenterShape;
                    var bordershape = quhua[0].BorderShape;
                    console.log("centershape x :" + centershape.X);
                    //创建一个图层
                    var graphicsLayer = new GraphicsLayer();
                    //设置可见
                    graphicsLayer.visible = true;
                    //创建一个点
                    var point = {
                        type: "point",
                        //x: 118.13565,
                        //y: 36.1654,
                        x: centershape.X,
                        y: centershape.Y,
                        SpatialReference: 4490
                    };
                    var symbol = {
                        type: "picture-marker",
                        url: "../../Images/exit.png",
                        width: "50px",
                        height: "50px",
                        outline: {
                            style: "solid"
                        },
                    };
                    var popupTemplate1 = {
                        title: "这是点的标题",
                        content: "<div>内容<div/>"
                    };

                    var graphic = new Graphic({
                        geometry: point,
                        symbol: symbol,
                        popupTemplate: popupTemplate1
                    });
                    var ringl = bordershape.polygon.length;
                    var _rings = new Array();
                    console.log("bordershape row is " + bordershape.polygon.length);
                    for (var i = 0; i < ringl; i++){
                        console.log("bordershape " + bordershape.polygon[i]);
                        var shape = new Array(bordershape.polygon[i].X, bordershape.polygon[i].Y);
                        _rings[i] = shape;
                    }
                    //创建一个面
                    var polygon = {
                        type: "polygon",
                        rings: _rings
                    };
                    var fillSymbol = {
                        type: "simple-fill",
                        color: [227, 139, 79, 0.5],
                        outline: {
                            color: [255, 255, 255],
                            width: 1
                        }
                    };
                    var popupTemplate2 = {
                        title: "这是面的标题",
                        content: "<div>这是一个绘制的面<div/>"
                    };
                    var polygonGraphic = new Graphic({
                        geometry: polygon,
                        symbol: fillSymbol,
                        popupTemplate: popupTemplate2
                    });
                    //添加多个要素
                    graphicsLayer.graphics.addMany([graphic, polygonGraphic]);
                    //添加单个要素
                    //graphicsLayer.graphics.add(graphic);
                    //添加单个图层
                    window.myMap.add(graphicsLayer);
                    console.log("画的面是：" + quhua[0].MingCheng);

                }
            });


           
        });

}

function showSheBei(fhdwid, authority, sblxid) {
    switch (sblxid) {
        case 1:
            setInterval(function () {
                getSheBei(fhdwid, authority,1);
                console.log("手持终端更新");
            }, 13 * 1000); // 每隔毫秒执行一次
            break;
        case 2:
            setInterval(function () {
                getSheBei(fhdwid, authority,2);
                console.log("车载终端更新");
            }, 3 * 1000); // 每隔毫秒执行一次
            break;
    }

}
function getSheBei(fhdwid, authority,sblxid) {
    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/Basemap",
        "esri/core/urlUtils",
        "esri/geometry/Extent",
        "esri/geometry/Point",
        "esri/layers/GraphicsLayer",
        "esri/layers/MapImageLayer",
        "esri/views/MapView",
        "esri/Graphic",
        "esri/Color",
        "esri/symbols/PictureFillSymbol",
        "esri/geometry/SpatialReference",
    ], function (Map,
        MapView,
        Basemap,
        urlUtils,
        Extent,
        Point,
        GraphicsLayer,
        MapImageLayer,
        MapView,
        Graphic,
        Color,
        PictureFillSymbol,
        SpatialReference) {
        
        function getGraphicsLayer(layerid) {
            for (var i = 0; i < window.myMap.allLayers.items.length; i++) {
                console.log("遍历的Layer Id:---" + i + "、" + window.myMap.allLayers.items[i].id + "===" + layerid);
                if (window.myMap.allLayers.items[i].id === layerid) {
                    return window.myMap.allLayers.items[i];
                }
            }
            return null;
        }
        


        $.ajax({
            url: '/ashxs/getSheBeiByFangHuoDanWeiId.ashx',
            type: 'GET',
            data: {
                "fhdwid": fhdwid,
                "authority": authority,
                "sblxid": sblxid
            },
            success: function (data) {
                var sbs = JSON.parse(data);


                //删除旧图层
                var layer = getGraphicsLayer("tuceng" + sblxid);
                console.log("删除前layer count:" + window.myMap.allLayers.items.length + "\r\n=====================");
                //window.myMap.allLayers.items.remove(layer);
                window.myMap.remove(layer);
                console.log("删除后layer count:" + window.myMap.allLayers.items.length + "\r\n=====================");
                //创建一个图层
                var graphicsLayer = new GraphicsLayer();
                //设置可见
                graphicsLayer.visible = true;
                graphicsLayer.id = "tuceng" + sblxid;



                console.log("获取设备数量 :" + sbs.length);
                for (var i = 0; i < sbs.length; i++) {

                    //创建一个点
                    var point = {
                        type: "point",
                        x: sbs[i].Shape.X,
                        y: sbs[i].Shape.Y,
                        SpatialReference: 4490
                    };
                    if (sblxid == 1) {
                        var symbol = {
                            type: "picture-marker",
                            url: "../../Images/MapImages/shouchi.png",
                            width: "24px",
                            height: "24px",
                            outline: {
                                style: "solid"
                            },
                        };
                    } else {
                        var symbol = {
                            type: "picture-marker",
                            url: "../../Images/MapImages/chezai.png",
                            width: "24px",
                            height: "24px",
                            outline: {
                                style: "solid"
                            },
                        };
                    }
                    var popupTemplate1 = {
                        title: "这是点的标题",
                        content: "<div>内容 --"+sbs[i].ShiYongRen+"-- <a href='https://www.baidu.com'>百度</a><div/>"
                    };

                    var graphic = new Graphic({
                        geometry: point,
                        symbol: symbol,
                        attributes: {
                            ENNAME: sbs[i].ShiYongRen,
                            OBJECTID: sbs[i].Id,
                            XG: "dgq",
                            deleteFlag: true,
                        },
                        popupTemplate: popupTemplate1
                    });
                    //添加单个要素
                    graphicsLayer.graphics.add(graphic);
                }
                //添加单个图层
                window.myMap.add(graphicsLayer);
            },
            error: function (textStatus, errorThrown) {
                console.error('Error:', textStatus, errorThrown);
            }
        });

    });
}